package com.insta.TP1;

import java.util.*;

/**
 *
 * @author Sebastien
 */
public class Person
{

    private String _name;
    private String _surName;
    private int _yearOfBirth;

    /**
     * ************************************************************************
     *
     * Constructor(s) for the class.
     *
     * /************************************************************************
     */
    public Person()
    {
        this("", "", 0);
    }

    public Person(String name)
    {
        this(name, "", 0);
    }

    public Person(String name, String surName)
    {
        this(name, surName, 0);

    }

    public Person(String name, String surName, int yearOfBirth)
    {
        setName(name);
        setSurName(surName);
        setYearOfBirth(yearOfBirth);
    }

    /**
     * ************************************************************************
     *
     * Getters and setters for the Name
     *
     * /************************************************************************
     */
    
    /**
     * Gets the name of the Person.
     *
     * @return the name of the Person.
     */
    public String getName()
    {
        return _name;
    }

    public void setName(String name)
    {
        this._name = name.toUpperCase();
    }

    /**
     * ************************************************************************
     *
     * Getters and setters for the SurName
     *
     * /************************************************************************
     */
    /**
     *
     * @return
     */
    public String getSurName()
    {
        return _surName;
    }

    public void setSurName(String surName)
    {
        if (surName.length() > 10)
        {
            surName = surName.substring(0, 10);
        }

        this._surName = surName;
    }

    /**
     * ************************************************************************
     *
     * Getters and setters for the Year of Birth
     *
     * /************************************************************************
     */
    /**
     * Returns the year of birth
     *
     * @return the year of birth
     */
    public int getYearOfBirth()
    {
        return _yearOfBirth;
    }

    public void setYearOfBirth(int newYOB)
    {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);

        // If the year of birth is incorrect..
        if ((newYOB > currentYear) || (newYOB < currentYear - 100))
        {
            _yearOfBirth = -1;
        } else
        {
            _yearOfBirth = newYOB;
        }
    }

    public int getAge()
    {
        int result = 0;

        // if the year of birth is incorrect..
        if (_yearOfBirth == -1)
        {
            result = -1;
        } else
        {
            int currentYear = Calendar.getInstance().get(Calendar.YEAR);

            result = currentYear - _yearOfBirth;
        }
        return result;
    }

}
